
SRCS=$(wildcard *.cc)
PROTOCS=$(wildcard *.proto)
PROTO_SRCS=$(wildcard *.pb.cc)
BIN_SRCS=$(shell grep "int main" -l *.cc)
LIB_SRCS=$(patsubst $(BIN_SRCS),,$(SRCS)) 

DEPS=$(SRCS:.cc=.d) 
OBJS=$(SRCS:.cc=.o) $(PROTO_SRCS:.cc=.o)

LIB_OBJS=$(patsubst %.cc,%.o,$(LIB_SRCS))  $(PROTO_SRCS:.cc=.o)
BINS=$(patsubst %.cc,%,$(BIN_SRCS))

LIB_NAME=drizzle
SO_LIB=$(patsubst %,lib%.so,$(LIB_NAME))
STATIC_LIB=$(patsubst %,lib%.a,$(LIB_NAME))


CC=$(DISTCC) g++
CFLAGS= -Wall -D_REENTRANT -D_GNU_SOURCE -fPIC
CFLAGS += -std=c++11
BOOST_DIR = /usr/local/include/boost/
THRIFT_DIR = /usr/local/include/thrift
#INC= -I${THRIFT_DIR} -I${BOOST_DIR}  -I. -I../public 
INC= -I. -I..

ifeq ($(release), 1)
CFLAGS += -O2 -DNDEBUG 
else
CFLAGS += -g -DDEBUG 
endif

all: $(BINS) $(SO_LIB) $(STATIC_LIB) 

$(BINS): % : %.o $(LIB_OBJS) 
	$(CC) $(CFLAGS) -o $@ $^  
$(SO_LIB): $(LIB_OBJS) 
	$(CC) $(CFLAGS) -shared -o $@ $^ 
$(STATIC_LIB): $(LIB_OBJS) 
	ar -rcs -o $@ $^ 
dist :
	rm -f $(DEPS) *.o
clean:
	rm -f $(BINS) $(OBJS) $(DEPS) $(SO_LIB) $(STATIC_LIB)
release: clean
	@make release=1

%.d : %.cc
	$(CC) $(CFLAGS) -MT $(subst .cc,.o,$<) -MM $(INC)  $< >$@
%.o : %.cc
	$(CC) $(CFLAGS) $(INC) -c  $< 

%.pb.o : %.pb.cc
	$(CC) $(CFLAGS) $(INC) -c  $< 
proto :
	protoc --cc_out=. *.proto
	protoc  --plugin=/home/gxl/proto-gen-as3/protoc-gen-as3 --as3_out=as3 *.proto 

-include  $(DEPS)

